home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / acrx / sample / sqr.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  6.9 KB  |  224 lines

  1. /* Next available MSG number is   6 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  sqr.cc
  5.  
  6.    Copyright (C) 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:Another Sample RXADS application
  29.  
  30.    Author     :  
  31.                  Autodesk, Inc.
  32.                  2320 Marinship Way
  33.                  Sausalito, CA. 94965
  34.                  (415)332-2344
  35.  
  36.     This Arx application is a conversion from the original sample ADS app
  37.     sqr.c.
  38.  
  39.     Original creation - by Mark Barrow - 3/14/90
  40.     Comments and revisions - Randy Clark, 4/23/90
  41.     Converted to Arx by:  William Howison, January 1994
  42.  
  43.     Function Entry Points:
  44.       AcRx::AppRetCode
  45.         acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt);
  46.  
  47.     Notes and restrictions on use:
  48.  
  49.  
  50. ***************************************************************************/
  51.  
  52.  
  53. /**************************************************************************/
  54. /*  MODULE NAME  */
  55. /**************************************************************************/
  56. #define    SQR
  57.  
  58.  
  59. /**************************************************************************/
  60. /*  INCLUDES  */
  61. /**************************************************************************/
  62.  
  63. #include <stdio.h>
  64. #include <math.h>
  65. #include "adslib.h"
  66. #include "rxdefs.h"
  67. #include "adesk.h"
  68. #include "ol_errno.h"
  69.  
  70. extern "C" {
  71. /****************************************************************************/
  72. /*  LOCALLY DEFINED ENTRY POINT INVOKED BY Arx                              */
  73. /****************************************************************************/
  74. AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void*);
  75. }
  76.  
  77. #define ExtFuncCount    1             /* Must increase this count if new
  78.                                          external functions are added */
  79.  
  80. /****************************************************************************/
  81. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  82. /****************************************************************************/
  83. int funcload _((void));
  84. int funcunload _((void));
  85. int dofun _((void));
  86.  
  87. /**************************************************************************/
  88. /*  GLOBAL VARIABLES  */
  89. /**************************************************************************/
  90. char *exfun[] = {/*MSG0*/"sqr"};      /* No "C:" -- to be called as an AutoLISP
  91.                                          function, not as an AutoCAD command */
  92.  
  93. /* More functions could be added to this table.  For example:
  94.    char *exfun[] = {"C:sqr", "C:shaft", "PIN"}; */
  95.  
  96. /*-----------------------------------------------------------------------*/
  97. /* FUNCLOAD  --  Define this application's external functions  */
  98.  
  99. int funcload()
  100. {
  101.     int i;
  102.     for (i = 0; i < ExtFuncCount; i++) {
  103.         if (!ads_defun(exfun[i], i))
  104.             return RTERROR;
  105.     }
  106.     return RTNORM;
  107. }
  108. /*-----------------------------------------------------------------------*/
  109. /* FUNCUNLOAD  --  Unload external functions */
  110.  
  111. int funcunload()
  112. {
  113.     int i;
  114.  
  115.     /* Undefine each function we defined */
  116.  
  117.     for (i = 0; i < ExtFuncCount; i++) {
  118.         ads_undef(exfun[i],i);
  119.     }
  120.     return RTNORM;
  121. }
  122. /*-----------------------------------------------------------------------*/
  123. /* DOFUN -- Execute external function (called upon an RQSUBR request) */
  124.  
  125. int dofun()
  126. {
  127.     struct resbuf *rb;
  128.     int val;
  129.     ads_real x;
  130.     extern ads_real rsqr _((ads_real));
  131.  
  132.     if ((val = ads_getfuncode()) < 0)
  133.         return 0;
  134.  
  135.     if ((rb = ads_getargs()) == NULL)
  136.         return 0;
  137.  
  138.     switch (val) {                    /* The 'sqr' function was defined
  139.                                          to have a funcode of 0 */
  140.  
  141.     case 0:
  142.         if (rb->restype == RTSHORT) {   /* Save in local variable */
  143.             x = (ads_real) rb->resval.rint;
  144.         } else if (rb->restype == RTREAL) {
  145.             x = rb->resval.rreal;     /* Can accept either real
  146.                                          or integer */
  147.         } else {
  148.             ads_fail(/*MSG3*/"Argument should be a real or integer value.");
  149.             return 0;
  150.         }
  151.         if (x < 0) {                  /* Check argument range */
  152.             ads_fail(/*MSG4*/"Argument should be positive.");
  153.             return 0;
  154.         }
  155.  
  156.         ads_retreal(rsqr(x));         /* Call the function itself, and
  157.                                          return the value to AutoLISP */
  158.  
  159.         return 1;
  160.  
  161.     default:
  162.         ads_fail(/*MSG5*/"Received nonexistent function code.");
  163.         return 0;
  164.     }
  165. }
  166. /*-----------------------------------------------------------------------*/
  167. /* This is the implementation of the actual external function */
  168.  
  169. ads_real rsqr(ads_real x)       /* Figure square root (Newton-Raphson method) */
  170. {
  171.     ads_real y, c, cl;
  172.     int n;
  173.  
  174.     if (x == 0.0) {
  175.         return 0.0;
  176.     }
  177.  
  178. #ifdef __STDC__
  179.     y = frexp(x, &n);        /* split x to get exponent */
  180.     y = ldexp(1.0, n / 2);   /* good guess at sqrt */
  181.     n = 10;                  /* it will converge faster than this */
  182. #else
  183.     y = (0.154116 + 1.893872 * x) / (1.0 + 1.047988 * x);
  184.     /* It takes a lot of iterations to get sqrt(1E22) from
  185.        an initial guess of 1.9 */
  186.     n = 50;
  187. #endif
  188.     c = 0.0;
  189.     do {
  190.         cl = c;
  191.         c = (y - x / y) * 0.5;
  192.         y -=  c;
  193.     } while (c != cl && --n);
  194.     return y;
  195. }
  196.  
  197. /* =================== Arx Module Interface Functions ================ */
  198.  
  199.  
  200. AcRx::AppRetCode
  201. /*FCN*/acrxEntryPoint(AcRx::AppMsgCode msg, void * )
  202. {
  203.  
  204.     switch(msg) {
  205.         case AcRx::kInitAppMsg:
  206.             break;
  207.         case AcRx::kInvkSubrMsg:
  208.             dofun();
  209.             break;
  210.         case AcRx::kLoadADSMsg:
  211.             funcload();
  212.             break;
  213.         case AcRx::kUnloadADSMsg:
  214.             funcunload();
  215.             ads_printf(/*MSG2*/"Unloading.\n");
  216.             break;
  217.         case AcRx::kUnloadAppMsg:
  218.         default:
  219.             break;
  220.     }
  221.     return AcRx::kRetOK;
  222. }
  223.  
  224.